// pages/other/user-info/index.js
const cloudHelper = require('../../../helper/cloud_helper.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        nickname: '微信用户', // 昵称
        realname: '', // 真实姓名
        phone: '', // 手机号
        avatar: '', // 头像
        sex: '', // 0:女 1:男
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.getUserInfo();
    },

    // 获取手机号
    async getPhoneNumber(e) {
        if (e.detail.errMsg != "getPhoneNumber:ok") {
            wx.showToast({
                title: '授权失败',
                icon: 'error'
            });
            return;
        }
        let params = {
            cloudID: e.detail.cloudID
        };
        let res = await cloudHelper.callCloudData('api/common/getwxphone', params);
        if (res.phone) {
            this.setData({
                phone: res.phone
            })
        }
    },

    // 获取头像
    async getAvatar(e) {
        let avatarUrl = e.detail.avatarUrl;
        // 上传头像
        wx.showLoading({
            title: '头像上传中'
        });
        let pic = await cloudHelper.transTempPicOne(avatarUrl, 'user/', '', false);
        wx.hideLoading();
        if (!pic) {
            wx.showToast({
                title: '头像上传失败',
                icon: 'error'
            });
            return;
        }
        this.setData({
            avatar: pic
        });
    },

    // 修改性别
    changeSex(e) {
        console.log(e);
        let sex = e.detail.value;
        this.setData({
            sex: sex
        });
    },

    // 获取用户信息
    async getUserInfo() {
        let userInfo = await cloudHelper.callCloudData('api/user/get_info');
        if (userInfo.phone) {
            this.setData({
                nickname: userInfo.nickname,
                realname: userInfo.realname,
                phone: userInfo.phone,
                avatar: userInfo.avatar,
                sex: userInfo.sex
            });
        }
    },

    // 提交资料
    async submitInfo() {
        let nickname = this.data.nickname;
        let realname = this.data.realname;
        let phone = this.data.phone;
        let avatar = this.data.avatar;
        let sex = this.data.sex;
        if (!nickname) {
            wx.showToast({
                title: '请输入昵称',
                icon: 'error'
            });
            return;
        }
        if (!realname) {
            wx.showToast({
                title: '请输入真实姓名',
                icon: 'error'
            });
            return;
        }
        if (!phone) {
            wx.showToast({
                title: '请授权手机号',
                icon: 'error'
            });
            return;
        }
        if (!avatar) {
            wx.showToast({
                title: '请上传头像',
                icon: 'error'
            });
            return;
        }
        if (!sex) {
            wx.showToast({
                title: '请选择性别',
                icon: 'error'
            });
            return;
        }
        // 提交资料
        let params = {
            nickname,
            realname,
            phone: phone,
            avatar: avatar,
            sex: sex,
        };
        let res = await cloudHelper.callCloudData('api/user/edit_info', params);
        if (res.status == 1) {
            wx.showToast({
                title: '修改成功'
            });
            // 获取上一页面
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.getUserInfo();
            setTimeout(() => {
                wx.navigateBack({
                    refresh: 1
                });
            }, 2000);
        } else {
            wx.showToast({
                title: '修改失败',
                icon: 'error'
            });
        }
    },
})